package com.example.gastometro;



import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class GastosActivity extends ListActivity { 
	ListAdapter adapter; 
    DbAdapter datasource;

    int idGasto;
    Gasto gasto;
 
	@SuppressWarnings("deprecation")
	@Override 
    public void onCreate(Bundle savedInstanceState) { 
             super.onCreate(savedInstanceState); 
             setContentView(R.layout.activity_gastos); 
             
             datasource = new DbAdapter(this); 
             datasource.open(); 
             Cursor cursor = datasource.getGastos(); 
             
             String[] columns = new String[] { "nome","local" }; 
             int[] to = new int[] { R.id.nome, R.id.local}; 
             
             adapter = new SimpleCursorAdapter(this,
            		 R.layout.activity_detalhe_na_list_gastos, 
                     cursor, 
                     columns, 
                     to); 
                     this.setListAdapter(adapter); 
                     datasource.close(); 
            } 
   
	@SuppressWarnings("deprecation")
	@Override
          protected void onResume() {
        	// TODO Auto-generated method stub
        	super.onResume();
        	datasource.open();
        	Cursor cursor = datasource.getGastos();
        	String[] columns = new String[] { "nome","local" };
        	int[] to = new int[] { R.id.nome, R.id.local};
        	adapter = new SimpleCursorAdapter(this, R.layout.activity_detalhe_na_list_gastos, cursor, columns, to);
        	
        	}
          @Override
          protected void onListItemClick(ListView l, View v, int position, long id) {
        	 
  			
     
      	
      		
        	  Cursor cursor = (Cursor) adapter.getItem(position);
        
					// TODO Auto-generated method stub
					AlertDialog.Builder dialogo = new AlertDialog.Builder(this);
					dialogo.setTitle("Aviso");
					dialogo.setMessage("Eliminar Gasto?");				
			dialogo.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {  
			public void onClick(DialogInterface dialog, int id) {  			 
					}  
			});
			dialogo.setPositiveButton("Eliminar",new DialogInterface.OnClickListener() {  
				
					public void onClick(DialogInterface dialog, int id) {
						datasource.open();
						datasource.EliminaGasto(idGasto);
						 datasource.close();
			    	 		   finish();
					}  
				});
				dialogo.show();
				}
			}


	
          

